//
//  LLTabBarController.m
//  Weibo
//
//  Created by 李灵斌 on 15-1-28.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "LLTabBarController.h"
#import "LLHomeController.h"
#import "LLMeController.h"
#import "LLMessageController.h"
#import "LLDiscoverController.h"
#import "LLNavigationController.h"
#import "LLTabBar.h"
#import "LLTabBar2.h"
#import "LLComposeController.h"
@interface LLTabBarController ()<LLTabBar2Delegate>
@property (nonatomic, weak) LLTabBar2 *customTabbar;
@end

@implementation LLTabBarController


- (instancetype)init{
    
    if (self = [super init]) {

    }
    return self;
}

// 该方法会在init方法中调用
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        
        // 首页
        LLHomeController *home = [[LLHomeController alloc] init];
        [self addController:home title:@"首页" image:@"tabbar_home"];
        
        // 消息
        LLMessageController *message = [[LLMessageController alloc] init];
        [self addController:message title:@"消息" image:@"tabbar_message_center"];
        
        // 发现
        LLDiscoverController *discover = [[LLDiscoverController alloc] init];
        [self addController:discover title:@"发现" image:@"tabbar_discover"];
        
        // 我
        LLMeController *me = [[LLMeController alloc] init];
        [self addController:me title:@"我" image:@"tabbar_profile"];
        
        
    }
    return self;
}

#pragma mark - 添加子控制器
- (void)addController:(UIViewController *)vc title:(NSString *)title image:(NSString *)image;{
    

    vc.tabBarItem.image = [UIImage imageNamed:image];
    NSString *selectedStr = [NSString stringWithFormat:@"%@_selected", image];
    UIImage *selectedImage = [UIImage imageNamed:selectedStr];
    UIImage *newSelectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    vc.tabBarItem.selectedImage = newSelectedImage;
    vc.title = title;
    
    // 设置字体选中的颜色
    [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : LLGlobalTextColor} forState:UIControlStateSelected];
    LLNavigationController *nav = [[LLNavigationController alloc] initWithRootViewController:vc];
    [self addChildViewController:nav];
    
    [self.customTabbar addItem:vc.tabBarItem];
    
    
}

#pragma mark - 控制器的生命周期方法
- (void)viewDidLoad{
    
    [super viewDidLoad];
    
    // 利用kvc将控制的属性更改为自定义的属性
//    LLTabBar *customTabbar = [[LLTabBar alloc] init];
//    [self setValue:customTabbar forKeyPath:@"tabBar"];
    
    LLTabBar2 *customTabbar = [[LLTabBar2 alloc] init];
    customTabbar.frame = self.tabBar.frame;
    customTabbar.delegate = self;
    [self.view addSubview:customTabbar];
    [self.tabBar removeFromSuperview];
    self.customTabbar = customTabbar;
}

#pragma mark - LLTabBar2Delegate
- (void)tabBar2:(LLTabBar2 *)tabbar form:(NSInteger)from to:(NSInteger)to{
    
    // 切换控制器
    self.selectedIndex = to;

}

- (void)tabBar2:(LLTabBar2 *)tabbar btnClick:(UIButton *)btn{
    
    LLComposeController *composeVc = [[LLComposeController alloc] init];
    LLNavigationController *nav = [[LLNavigationController alloc] initWithRootViewController:composeVc];
    [self presentViewController:nav animated:YES completion:nil];
}


@end
